package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/3 10:07
 * @description：https://leetcode-cn.com/problems/day-of-the-week/
 */
public class 一周中的第几天 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.dayOfTheWeek(31, 8, 2019));
    }

    /*
    class Solution {
        public String dayOfTheWeek(int day, int month, int year) {
            String[] ans = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
            Calendar calendar = new GregorianCalendar();
            calendar.set(year, month - 1, day);
            return ans[calendar.get(Calendar.DAY_OF_WEEK) - 1];
        }
    }
     */

    class Solution {
        public String dayOfTheWeek(int day, int month, int year) {
            String[] weeks = new String[]{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
            int[] months = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
            // 1971年以前第一个闰年是1968，所以是year - （1968 + 1)
            int days = 365 * (year - 1971) + (year - 1969) / 4;
            for (int i = 0; i < month - 1; i++) {
                days += months[i];
            }
            if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month > 2) {
                day++;
            }
            days += day;
            return weeks[(days + 3) % 7];
        }
    }
}
